package com.ck.jdk8.stream.service;

import com.ck.jdk8.stream.dao.EsProductDao;
import com.ck.jdk8.stream.domain.EsProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Classname ListGroup
 * @Description list分组
 * @Date 2020/5/28 17:08
 * @Created by lpc
 */
@Service
public class ListGroup {

    @Autowired
    private EsProductDao productDao;

    public void inspectionVideoEvaluation() {

        List<Map<String, Object>> list = productDao.esProductList(null);

        //    1. 跟据某个属性分组OfficeId：
        Map<Object, List<Map<String, Object>>> collect = list.stream().collect(Collectors.groupingBy(m -> m.get("brandId")));

        System.out.println(collect);



//    2. 根据某个属性分组OfficeId，汇总某个属性Money：
        List<EsProduct> esProductList = productDao.getAllEsProductList(null);

//        Map<String, Double> collect1 = list.stream().collect(Collectors.groupingBy(EsProduct::getBrandId,Collectors.summingDouble(EsProduct::getPrice)));



//    3. 根据某个属性添加条件过滤数据：
        List<EsProduct> list1 = esProductList.stream().filter(u -> !u.getPrice().equals("0.00")).collect(Collectors.toList());
        System.out.println("根据某个属性添加条件过滤数据："+list1);


//    4. 判断一组对象里面有没有属性值是某个值：
//        List<Menu> menuList = UserUtils.getMenuList();
//        boolean add = menuList.stream().anyMatch(m -> "plan:ctPlan:add".equals(m.getPermission()));


//    5. 取出一组对象的某个属性组成一个新集合：
        List<String> tableNames=esProductList.stream().map(EsProduct::getBrandName).collect(Collectors.toList());


//    6. list去重复：
        list = list.stream().distinct().collect(Collectors.toList());

        //去重
        List<Map<String, Object>> maptime = list.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(m -> m.get("brandId").toString())) ), ArrayList::new
                )
        );

    }


}
